/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Helper.ConnectionHelper;
import common.Constant;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author KIENLT
 */
public class ConnectionDAO {
    //static Connection cnn = ConnectDatabase.ConnectDB(null, null, null, null)
    public int Insert(Connection cnn, ConnectionHelper obj){
        try {
            String strSQL = "{call insertConnect(?,?,?,?)}";
            CallableStatement stmt = cnn.prepareCall(strSQL);
            stmt.setString(1, obj.getDatabase());
            stmt.setString(2, obj.getUserName());
            stmt.setString(3, obj.getPassword());
            stmt.setString(4, obj.getPost());
            stmt.executeUpdate();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Insert Fail.", "Error", JOptionPane.ERROR_MESSAGE);
            return 0;
        }
        return 1;
    }
    
    public HashMap<String, String> fillAll(Connection cnn){
        HashMap<String, String> listAll = new HashMap<String, String>();
        String strSQL = "{call fillAllConnection}";
        try {
            CallableStatement stmt = cnn.prepareCall(strSQL);
            ResultSet rs = stmt.executeQuery();
            while(rs.next()){
                listAll.put("CDatabase", rs.getString("CDatabase"));
                listAll.put("CLogin", rs.getString("CLogin"));
                listAll.put("CPassword", rs.getString("CPassword"));
                listAll.put("CPost", rs.getString("CPost"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(ConnectionDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return listAll;
    }
}
